/**
 * Created by Jerry on 16/2/1.
 */
laAir.controller('laAir_airlineListPageCtl', ['$window', '$document', '$scope','$interval','$filter', 'laUserService', 'laFlightService', 'laGlobalLocalService',function ($window, $document, $scope,$interval,$filter,laUserService, laFlightService, laGlobalLocalService) {
    //是否选择了去程
    $scope.singleStatu=false;
    //去程是否选择保险
    $scope.singleInsurStatu=false;
    //是否是往返程
    $scope.qu_mode = 0;
    //最终选定的航班信息
    $scope.bookOrderInfo;
    $scope.bookOrderInfo_g;
    $scope.bookOrderInfo_b;
    $scope.mmdddate;
    $scope.flighttime;
    $scope.StartCity="";
    $scope.EndCity="";
    $scope.StartCityCode="";
    $scope.EndCityCode="";
    $scope.UserInfo={};
    var postdata={
        Type:"Function",
        name:"UserInfo",
        FromPage:"frameContentBox",
        ToFrame:"main",
        Serial:""
    }
    laGlobalLocalService.getLocalCacheByFrame(postdata);
    //自动登陆
    $scope.isAutoLogin=false;

    //登陆
    $window.addEventListener('message', function (e) {
        if (e.data.To.Target == "frameContentBox"){
            if(e.data.Content=="error"){
                return;
            }else{
                $scope.UserInfo=JSON.parse(e.data.Content);
                laUserService.Login($scope.UserInfo.LoginName,$scope.UserInfo.Password, '', function (backData, status) {
                    var rs = backData;

                    if (rs.Code == laGlobalProperty.laServiceCode_Success && status == true&&$scope.UserInfo.LoginName&&$scope.UserInfo.Password) {
                            $scope.isAutoLogin=true;
                            laGlobalLocalService.setLocalCache(laGlobalProperty.laServiceConst_TransData_isAutoLogin, $scope.isAutoLogin);
                            if ($scope.isAutoLogin) {
                                laGlobalLocalService.setLocalCache(laGlobalProperty.laServiceConst_TransData_AutoLoginId, $scope.UserInfo.LoginName);
                                laGlobalLocalService.setLocalCache(laGlobalProperty.laServiceConst_TransData_AutoLoginPwd, $scope.UserInfo.Password);
                            }
                    } else {
                        return false;
                    }
                });
            }

        }else {
            return;
        }
    }, false);

    //航班查询结果
    $scope.flightResult;
    $scope.flightBackResult;
    //去程预定查询信息
    $scope.queryFliInfo = new laEntityFlight();
    //返程预定查询信息
    $scope.qu_mode=0;
    $scope.queryFliInfoBack = new laEntityFlight();
    var cookieQueryFli = laGlobalLocalService.getLocalCache(laGlobalProperty.laServiceConst_TransData_QueryTicket);
    if (cookieQueryFli != undefined) {
        $scope.queryFliInfo = JSON.parse(cookieQueryFli);
        $scope.StartCity=$scope.queryFliInfo.AirportFromCH;
        $scope.EndCity=$scope.queryFliInfo.AirportToCH;
        $scope.StartCityCode=$scope.queryFliInfo.AirportFrom;
        $scope.EndCityCode=$scope.queryFliInfo.AirportTo;
        var isRoundTrip = $scope.queryFliInfo.RoundTrip;
        $scope.qu_mode = (isRoundTrip == true) ? 1 : 0;
    }
    QueryTicket($scope.queryFliInfo);
    if (isRoundTrip) {
        $scope.queryFliInfoBack.AirportFrom=$scope.queryFliInfo.AirportTo;
        $scope.queryFliInfoBack.AirportTo=$scope.queryFliInfo.AirportFrom;
        $scope.queryFliInfoBack.AirportFromCH=$scope.queryFliInfo.AirportToCH;
        $scope.queryFliInfoBack.AirportToCH=$scope.queryFliInfo.AirportFromCH;
        $scope.queryFliInfoBack.DepartureTime=$scope.queryFliInfo.RoundTripTime;
        QueryTicketBack($scope.queryFliInfoBack);
    }
    /**
     * 搜索机票按钮点击事件
     */
    $scope.btnQueryTicketClick = function () {
        var sCity = $("#startCity").val();
        var eCity = $("#arriveCity").val();
        //var sCityCode = $("#startCity").attr("segnum");
        //var eCityCode = $("#endCity").attr("segnum");
        var sCityCode = $("#startCity").attr("segnum");
        var eCityCode = $("#arriveCity").attr("segnum");
        var sTime = $("#StartTime").val();
        var eTime = $("#EndTime").val();

        $scope.queryFliInfo.AirportFromCH = sCity;
        $scope.queryFliInfo.AirportToCH = eCity;
        $scope.queryFliInfo.AirportFrom = sCityCode;
        $scope.queryFliInfo.AirportTo = eCityCode;
        $scope.queryFliInfo.DepartureTime = sTime;
        $scope.queryFliInfo.RoundTripTime = eTime;

        //出发城市为空
        if (laGlobalLocalService.CheckStringIsEmpty($scope.queryFliInfo.AirportFromCH)) {
            bootbox.alert('请选择出发城市');
            return;
        }
        //到达城市为空
        if (laGlobalLocalService.CheckStringIsEmpty($scope.queryFliInfo.AirportToCH)) {
            bootbox.alert('请选择到达城市');
            return;
        }
        if ($scope.queryFliInfo.AirportFrom == $scope.queryFliInfo.AirportTo) {
            bootbox.alert('出发和到达城市不能相同');
            return;
        }

        //出发时间为空
        if (laGlobalLocalService.CheckStringIsEmpty($scope.queryFliInfo.DepartureTime)) {
            bootbox.alert('请选择出发时间');
            return;
        }
        if (!laGlobalLocalService.CheckDateFormat($scope.queryFliInfo.DepartureTime)) {
            bootbox.alert('请输入YYYY-MM-DD格式的日期');
            return;
        }
        //返程时间为空
        if ($scope.qu_mode == 1) {
            if (laGlobalLocalService.CheckStringIsEmpty($scope.queryFliInfo.RoundTripTime)) {
                bootbox.alert('请选择返程时间');
                return;
            }
            if (!laGlobalLocalService.CheckDateFormat($scope.queryFliInfo.RoundTripTime)) {
                bootbox.alert('请输入YYYY-MM-DD格式的日期');
                return;
            }
            if ($scope.queryFliInfo.RoundTripTime < $scope.queryFliInfo.DepartureTime) {
                bootbox.alert('返程时间不能在去程时间之前');
                return;
            }
            $scope.queryFliInfoBack.AirportFromCH = $scope.queryFliInfo.AirportToCH;
            $scope.queryFliInfoBack.AirportToCH = $scope.queryFliInfo.AirportFromCH;
            $scope.queryFliInfoBack.AirportFrom = $scope.queryFliInfo.AirportTo;
            $scope.queryFliInfoBack.AirportTo = $scope.queryFliInfo.AirportFrom;
            $scope.queryFliInfoBack.DepartureTime = $scope.queryFliInfo.RoundTripTime;
            laGlobalLocalService.setLocalCache(laGlobalProperty.laServiceConst_TransData_QueryTicket, JSON.stringify($scope.queryFliInfo));
        }

        $scope.city1name = sCity;
        $scope.city2name = eCity;

        QueryTicket($scope.queryFliInfo);
        if($scope.qu_mode == 1){
            setTimeout(QueryTicketBack($scope.queryFliInfoBack),5000);
        }
    };
    //查询单程机票
    function QueryTicket(queryFliInfo) {
        if(queryFliInfo.DepartureTime!=undefined){
            var departureTime = new Date(queryFliInfo.DepartureTime.replace(/-/g, "/"));
            var mm=$filter('date')(departureTime, 'MM-dd');
            queryFliInfo.DepartureTimeMonthDay = mm.substring(0,2)+"月"+mm.substring(3,6)+"日";
            queryFliInfo.DepartureTimeWeek = laGlobalLocalService.getWeekName(departureTime);
        }
        $scope.flightResult = null;
        laFlightService.QueryFlight(queryFliInfo, function (backData, status) {
            var rs = backData;
            if (rs.Code == laGlobalProperty.laServiceCode_Success) {

               /* $interval.cancel(timer);*/

                //$scope.flightResult = rs;
                $scope.flightResult = new laEntityFlightList();
                $scope.flightResult.Code = rs.Code;
                $scope.flightResult.Message = rs.Message;
                $scope.flightResult.LowPriceFlights = rs.LowPriceFlights;
                var n = rs.FlightList.length;
                var cabinNum = 0;
                for (var i = 0; i < n; i++) {
                    if (rs.FlightList[i].CabinInfoList.length > 0) {
                        $scope.flightResult.FlightList[cabinNum] = rs.FlightList[i];
                        var date1 = new Date(rs.FlightList[i].DepartureTime);  //开始时间
                        var date2 = new Date(rs.FlightList[i].ArriveTime);     //结束时间
                        var date3 = date2.getTime() - date1.getTime();   //时间差的毫秒数
                        //计算相差的年数
                        var years = Math.floor(date3 / (12 * 30 * 24 * 3600 * 1000));
                        //计算相差的月数
                        var leave = date3 % (12 * 30 * 24 * 3600 * 1000);
                        var months = Math.floor(leave / (30 * 24 * 3600 * 1000));
                        //计算出相差天数
                        var leave0 = leave % (30 * 24 * 3600 * 1000);
                        var days = Math.floor(leave0 / (24 * 3600 * 1000));
                        //计算出小时数
                        var leave1 = leave0 % (24 * 3600 * 1000);     //计算天数后剩余的毫秒数
                        var hours = Math.floor(leave1 / (3600 * 1000));
                        //计算相差分钟数
                        var leave2 = leave1 % (3600 * 1000);         //计算小时数后剩余的毫秒数
                        var minutes = Math.floor(leave2 / (60 * 1000));
                        //计算相差秒数
                        var leave3 = leave2 % (60 * 1000);       //计算分钟数后剩余的毫秒数
                        var seconds = Math.round(leave3 / 1000);
                        $scope.flightResult.FlightList[cabinNum].flighttimelist=hours+"小时"+minutes+"分钟";
                        cabinNum++;
                    }
                }

            } else {
                alert(rs.Message);
            }
           /* GetFlightDaylist(queryFliInfo.AirportFrom, queryFliInfo.AirportTo, new Date(queryFliInfo.DepartureTime.replace(/-/g, "/")));*/
        });
    }
    /**
     * 查询返程机票
     * @constructor
     */
    function QueryTicketBack(queryFliInfo) {
        if(queryFliInfo.DepartureTime!=undefined){
            var departureTime = new Date(queryFliInfo.DepartureTime.replace(/-/g, "/"));
            var mm=$filter('date')(departureTime, 'MM-dd');
            queryFliInfo.DepartureTimeMonthDay = mm.substring(0,2)+"月"+mm.substring(3,6)+"日";
            queryFliInfo.DepartureTimeWeek = laGlobalLocalService.getWeekName(departureTime);
        }
        $scope.flightBackResult = null;

        laFlightService.QueryFlight(queryFliInfo, function (backData, status) {
            var rs = backData;
            if (rs.Code == laGlobalProperty.laServiceCode_Success) {

                //$scope.flightResult = rs;
                $scope.flightBackResult = new laEntityFlightList();
                $scope.flightBackResult.Code = rs.Code;
                $scope.flightBackResult.Message = rs.Message;
                $scope.flightBackResult.LowPriceFlights = rs.LowPriceFlights;
                var n = rs.FlightList.length;
                var cabinNum = 0;
                for (var i = 0; i < n; i++) {
                    if (rs.FlightList[i].CabinInfoList.length > 0) {
                        $scope.flightBackResult.FlightList[cabinNum] = rs.FlightList[i];
                        var date1 = new Date(rs.FlightList[i].DepartureTime);  //开始时间
                        var date2 = new Date(rs.FlightList[i].ArriveTime);     //结束时间
                        var date3 = date2.getTime() - date1.getTime();   //时间差的毫秒数
                        //计算相差的年数
                        var years = Math.floor(date3 / (12 * 30 * 24 * 3600 * 1000));
                        //计算相差的月数
                        var leave = date3 % (12 * 30 * 24 * 3600 * 1000);
                        var months = Math.floor(leave / (30 * 24 * 3600 * 1000));
                        //计算出相差天数
                        var leave0 = leave % (30 * 24 * 3600 * 1000);
                        var days = Math.floor(leave0 / (24 * 3600 * 1000));
                        //计算出小时数
                        var leave1 = leave0 % (24 * 3600 * 1000);     //计算天数后剩余的毫秒数
                        var hours = Math.floor(leave1 / (3600 * 1000));
                        //计算相差分钟数
                        var leave2 = leave1 % (3600 * 1000);         //计算小时数后剩余的毫秒数
                        var minutes = Math.floor(leave2 / (60 * 1000));
                        //计算相差秒数
                        var leave3 = leave2 % (60 * 1000);       //计算分钟数后剩余的毫秒数
                        var seconds = Math.round(leave3 / 1000);
                        $scope.flightBackResult.FlightList[cabinNum].flighttimelist=hours+"小时"+minutes+"分钟";
                        cabinNum++;
                    }


                    if ($scope.flightBackResult.FlightList.length <= 0) {
                        $(".noflightsBack").show();
                        $(".airlineBack").hide();
                    }
                }

            } else {
                alert(rs.message);


            }
           /* GetFlightDaylistBack(queryFliInfo.AirportFrom, queryFliInfo.AirportTo, new Date(queryFliInfo.DepartureTime.replace(/-/g, "/")));*/
        });
    }
    /**
     * 提交订单
     * @constructor
     */
    $scope.btnBookingOrderClick = function (fliInfo, cabinInfo,InsurStatu) {
        if($scope.qu_mode == 1){
            if($scope.singleStatu!=true){
                bootbox.alert("请先选择去程航班");
                return false;
            }
        }
        var tmpFli = {
            "AirportFrom": fliInfo.AirportFrom,
            "AirportFromCH": fliInfo.AirportFromCH,
            "AirportTax": fliInfo.AirportTax,
            "AirportTo": fliInfo.AirportTo,
            "AirportToCH": fliInfo.AirportToCH,
            "ArriveTime": fliInfo.ArriveTime,
            "ChildAirportTax": fliInfo.ChildAirportTax,
            "ChildFuelTax": fliInfo.ChildFuelTax,
            "ChildOtherTax": fliInfo.ChildOtherTax,
            "InfantAirportTax": fliInfo.InfantAirportTax,
            "InfantFuelTax": fliInfo.InfantFuelTax,
            "InfantOtherTax": fliInfo.InfantOtherTax,
            "DepartureTime": fliInfo.DepartureTime,
            "Distance": fliInfo.Distance,
            "FlightNum": fliInfo.FlightNum,
            "FuelTax": fliInfo.FuelTax,
            "JiXing": fliInfo.JiXing,
            "JingTing": fliInfo.JingTing,
            "OtherTax": fliInfo.OtherTax,
            "flighttimelist":fliInfo.flighttimelist,
            "AllFlightInfo":fliInfo.AllFlightInfo,
            "CabinName":fliInfo.CabinInfoList[0].CabinName,
        };
        //tmpFli.CabinInfoList = null;
        var tmpCab = cabinInfo;
        var otherInfo;
        var fli = new laEntityFlight();
        fli.AirportFrom = tmpFli.AirportFrom;
        fli.AirportTo = tmpFli.AirportTo;
        fli.AirportFromCH = tmpFli.AirportFromCH;
        fli.AirportToCH = tmpFli.AirportToCH;
        fli.DepartureTime = $scope.queryFliInfo.DepartureTime;
        fli.RoundTripTime = $scope.queryFliInfo.RoundTripTime;
        fli.RoundTrip = ($scope.qu_mode == 1) ? true : false;

        laGlobalLocalService.setLocalCache(laGlobalProperty.laServiceConst_TransData_QueryTicket, JSON.stringify(fli));
        if ($scope.qu_mode == 0) {
            otherInfo = {
                "sCity": $scope.StartCity,
                "eCity": $scope.EndCity,
                "sCityCode": $scope.StartCityCode,
                "eCityCode": $scope.EndCityCode,
                "week": laGlobalLocalService.getWeekName(new Date(fliInfo.DepartureTime)),
                "InsuranceStatu":InsurStatu
            };
            $scope.bookOrderInfo_g = {"f": tmpFli, "c": tmpCab, "o": otherInfo};
            $scope.mmdddate=$scope.bookOrderInfo_g.f.DepartureTime;
            $scope.bookOrderInfo_b = {};
        } else if ($scope.qu_mode == 1) {
            otherInfo = {
                "sCity": $scope.EndCity,
                "eCity": $scope.StartCity,
                "sCityCode": $scope.EndCityCode,
                "eCityCode": $scope.StartCityCode,
                "week": laGlobalLocalService.getWeekName(new Date(fliInfo.DepartureTime)),
                "InsuranceStatu":InsurStatu
            };
            $scope.bookOrderInfo_b = {"f": tmpFli, "c": tmpCab, "o": otherInfo};
        }
        $scope.bookOrderInfo = {"g": $scope.bookOrderInfo_g, "b": $scope.bookOrderInfo_b, "roundtrip": $scope.qu_mode};
        laUserService.GetCurrentUserInfo(function (backData, status) {
            var rs = backData;
            if (rs.Code == laGlobalProperty.laServiceCode_Success && rs.SessionOut == false) {
                laGlobalLocalService.setLocalCache(laGlobalProperty.laServiceConst_TransData_BookOrder, JSON.stringify($scope.bookOrderInfo));
                var PageData={
                    Type:"Function",
                    name:"提交订单",
                    ToFrame:"main",
                    Id:"PageticketDetail",
                    Url:"http://172.25.1.35:200/ticketDetails.html",
                    Serial:"",}
                laGlobalLocalService.gotoNewFrame(PageData)
            } else {
                $window.location.href = 'login.html';
            }
        });
    };
    /**
     * 去程航班选定按钮点击
     * @param fliInfo
     * @param cabinInfo
     */
    $scope.btnChooseFlightClick = function (fliInfo, cabinInfo,InsurStatu) {
        $scope.singleStatu=true;
        var tmpFli = {
            "AirportFrom": fliInfo.AirportFrom,
            "AirportFromCH": fliInfo.AirportFromCH,
            "AirportTax": fliInfo.AirportTax,
            "AirportTo": fliInfo.AirportTo,
            "AirportToCH": fliInfo.AirportToCH,
            "ArriveTime": fliInfo.ArriveTime,
            "ChildAirportTax": fliInfo.ChildAirportTax,
            "ChildFuelTax": fliInfo.ChildFuelTax,
            "ChildOtherTax": fliInfo.ChildOtherTax,
            "DepartureTime": fliInfo.DepartureTime,
            "Distance": fliInfo.Distance,
            "FlightNum": fliInfo.FlightNum,
            "FuelTax": fliInfo.FuelTax,
            "JiXing": fliInfo.JiXing,
            "JingTing": fliInfo.JingTing,
            "OtherTax": fliInfo.OtherTax,
            "flighttimelist":fliInfo.flighttimelist,
            "AllFlightInfo":fliInfo.AllFlightInfo,
            "CabinName":fliInfo.CabinInfoList[0].Name,
        };
        var mm=((new Date(fliInfo.DepartureTime)).getMonth()+1).toString();
        var dd=(new Date(fliInfo.DepartureTime)).getDate().toString();
        mm = (mm.length < 2) ? '0' + mm : mm;
        dd = (dd.length < 2) ? '0' + dd : dd;
        $scope.mmdddate=mm+"-"+dd;
        var date1 = new Date(fliInfo.DepartureTime);  //开始时间
        var date2 = new Date(fliInfo.ArriveTime);     //结束时间
        var date3 = date2.getTime() - date1.getTime();   //时间差的毫秒数
        //计算相差的年数
        var years = Math.floor(date3 / (12 * 30 * 24 * 3600 * 1000));
        //计算相差的月数
        var leave = date3 % (12 * 30 * 24 * 3600 * 1000);
        var months = Math.floor(leave / (30 * 24 * 3600 * 1000));
        //计算出相差天数
        var leave0 = leave % (30 * 24 * 3600 * 1000);
        var days = Math.floor(leave0 / (24 * 3600 * 1000));
        //计算出小时数
        var leave1 = leave0 % (24 * 3600 * 1000);     //计算天数后剩余的毫秒数
        var hours = Math.floor(leave1 / (3600 * 1000));
        //计算相差分钟数
        var leave2 = leave1 % (3600 * 1000);         //计算小时数后剩余的毫秒数
        var minutes = Math.floor(leave2 / (60 * 1000));
        //计算相差秒数
        var leave3 = leave2 % (60 * 1000);       //计算分钟数后剩余的毫秒数
        var seconds = Math.round(leave3 / 1000);
        $scope.flighttime=hours+"小时"+minutes+"分钟";

        //tmpFli.CabinInfoList = null;
        var tmpCab = cabinInfo;
        var otherInfo = {
            "sCity": $scope.StartCity,
            "eCity": $scope.EndCity,
            "sCityCode": $scope.StartCityCode,
            "eCityCode": $scope.EndCityCode,
            "week": laGlobalLocalService.getWeekName(new Date(fliInfo.DepartureTime)),
            "InsuranceStatu":InsurStatu
        };
        $scope.bookOrderInfo_g = {"f": tmpFli, "c": tmpCab, "o": otherInfo};
        $(".table-fli").show();
        $(".table-fli_go").hide();

        //查询返程
        /*fillFlightBackQueryInfo();*/
        /*QueryTicket($scope.queryFliInfoBack);*/
    };

}]);